package com.fan.configService.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fan.configService.exception.QhException;
import com.fan.configService.exception.QhUserErrors;
import com.fan.configService.po.Application;
import com.fan.configService.service.ApplicationService;
import com.fan.configService.util.ConvertUtil;
import com.fan.configService.util.ShiroUtils;
import com.fan.configService.controller.Request.ApplicationRequest;
import com.fan.configService.vo.ApplicationVo;
import com.fan.configService.vo.MyPage;
import com.fan.configService.vo.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * java类简单作用描述
 *
 * @Description: java类作用描述
 * @Author: hf
 * @CreateDate: 2018/9/5 15:59
 * @UpdateUser: hf
 * @UpdateDate: 2018/9/5 15:59
 * @UpdateRemark: The modified content
 * @Version: 1.0
 */


@Api(tags = "应用")
@Slf4j
@RestController
@RequestMapping("application")
public class ApplicationController {
    @Autowired
    ApplicationService applicationService;


    @ApiOperation(value = "根据id获取应用")
    @GetMapping("/getApplication/{applicationId}")
    public ResponseResult<ApplicationVo> getApplicationById(@PathVariable Long applicationId) {
        ResponseResult responseResult = new ResponseResult();
        ApplicationVo applicationVo = new ApplicationVo();
        Application application = applicationService.getApplicationById(applicationId);
        BeanUtils.copyProperties(application, applicationVo);
        responseResult.setData(applicationVo);
        return responseResult;
    }


    @ApiOperation(value = "根据id删除应用")
    @DeleteMapping("/delApplication/{applicationId}")
    public ResponseResult delApplication(@PathVariable Long applicationId) {
        applicationService.delApplication(applicationId);
        return new ResponseResult();
    }

    @ApiOperation(value = "根据id修改应用")
    @PutMapping("/updateApplication/{applicationId}")
    public ResponseResult updateApplication(@PathVariable @NotNull Long applicationId, @RequestBody @Valid ApplicationRequest applicationRequest) {
        Application application = new Application();
        BeanUtils.copyProperties(applicationRequest, application);
        application.setApplicationId(applicationId);
        applicationService.updateApplication(application);
        return new ResponseResult();
    }

    @ApiOperation(value = "添加应用")
    @PostMapping("/addApplication")
    public ResponseResult addApplication(@RequestBody ApplicationRequest applicationRequest) {
        Application application = new Application();
        BeanUtils.copyProperties(applicationRequest, application);
        Application applicationByName = applicationService.getApplicationByName(applicationRequest.getApplicationName());
        if (applicationByName != null) {
            throw new QhException(QhUserErrors.APPLICATION_EXIST);
        }
        applicationService.addApplication(application);
        return new ResponseResult();
    }

    @ApiOperation(value = "获取所有应用")
    @PostMapping("/getAllApplicationByPage")
    public ResponseResult<MyPage<ApplicationVo>> getAllApplicationByPage(@RequestBody MyPage myPage) {
        String userId = ShiroUtils.getUserId();
        log.info("userId:{}", userId);
        ResponseResult responseResult = new ResponseResult();
        IPage<Application> iPage = applicationService.getAllApplicationByPage(myPage, Long.parseLong(userId));
        List<ApplicationVo> applicationItemVoList = ConvertUtil.copyTo(iPage.getRecords(), ApplicationVo.class);
        myPage.setTotal(iPage.getTotal());
        myPage.setRecords(applicationItemVoList);
        responseResult.setData(myPage);
        return responseResult;
    }
}
